package com.muk.meis.controller.school;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import com.muk.meis.MEISConstance;
import com.muk.meis.model.bean.SchoolNew;
import com.muk.meis.model.bean.UserProfile;
import com.muk.meis.model.service.SchoolNewsService;
import com.muk.webframework.framework.controller.BaseController;

/**
 * Servlet implementation class NewsCreateController
 */
@WebServlet("/NewsController/*")
public class NewsController extends BaseController {
	private static final long serialVersionUID = 1L;
	SchoolNewsService service;

    /**
     * Default constructor. 
     */
    public NewsController() {
    	service = new SchoolNewsService();
    }
    
    public void list() throws IOException, ServletException {
    	System.out.println("list method..."); 
    	response.sendRedirect(request.getContextPath()+"/school/listnews.jsp?percode="+request.getParameter("percode"));
    }
    
    public void create() throws IOException{
    	System.out.println("Create method...");
    	String title = request.getParameter("title");
    	String message = request.getParameter("message");
    	String percodeCode = request.getParameter("percode");
    	UserProfile userPassLogin = (UserProfile)session.getAttribute(MEISConstance.USER_PASS_LOGIN);
    	SchoolNew schoolNews = new SchoolNew();
    	schoolNews.setTitle(title);
    	schoolNews.setMessage(message);
    	schoolNews.setPercodeCode(percodeCode);
    	schoolNews.setUserId(userPassLogin.getUserId());
    	schoolNews.setCreateDate(new Date());
 
    	service.create(schoolNews);
    	response.sendRedirect(request.getContextPath()+"/school/listnews.jsp?percode="+percodeCode);
    }
    
    public void update() throws IOException{
    	System.out.println("Update method...");
        String newsId = request.getParameter("newsid");
    	String title = request.getParameter("title");
    	String message = request.getParameter("message");    	
    	SchoolNew schoolNews = new SchoolNew();
    	schoolNews.setNewsId(Integer.parseInt(newsId));
    	schoolNews.setTitle(title);
    	schoolNews.setMessage(message);
    	schoolNews.setCreateDate(new Date());
    	
    	service.update(schoolNews);
    	String percodeCode = request.getParameter("percode");
    	response.sendRedirect(request.getContextPath()+"/school/listnews.jsp?percode="+percodeCode);
    }

}
